package leetcode.leetcode_110;

class Solution {
    public boolean isBalanced(TreeNode root) {
        return findDepth(root)!=-1;
    }
    public int findDepth(TreeNode root){
        if (root == null )return 0;
        int leftDepth = findDepth(root.left);
        if (leftDepth == -1) return -1;
        int rightDepth = findDepth(root.right);
        if (rightDepth == -1) return -1;
        if (Math.abs(rightDepth-leftDepth)>1) return -1;
        return Math.max(leftDepth,rightDepth)+1;
    }
}